<?php  
namespace app\library\admin;
use app\admin\controller\Admin;
use app\library\model\User as UserModel;
use app\library\model\Violation as ViolationModel;
//用户控制器
class User extends Admin{

	public function index(){
		$this->assign('tab_type', 3);
        if ($this->request->isAjax()) {
            $where = $data = [];
            $page = input('param.page/d', 1);
            $limit = input('param.limit/d', 15);
            $keyword = input('param.keyword');
            $status = input('param.status');
            if ($keyword) {
                $where['card_id'] = $keyword;
            }
            if (!empty($status) || $status === '0'){
                $where['status'] = $status;
            }
            $list = UserModel::where($where)->page($page)->limit($limit)->order('id desc')->select();
            foreach ($list as $key => $value) {
                $list[$key]['etime'] = date("Y-m-d",$value['etime']);
                switch ($value['group']) {
                	case '0':$group = '学生';break;
                	case '1':$group = '老师';break;
                	case '2':$group = '管理员';break;
                	default:$group = '无';;break;
                }
                $list[$key]['group'] = $group;
                $list[$key]['count'] = 0;
            }
            $data['data'] = $list;
            $data['count'] = UserModel::where($where)->count('id');
            $data['code'] = 0;
            $data['msg'] = '';
            return json($data);
        }
        return $this->fetch();
	}
    /**
     * 添加用户
     * @return mixed
     */
	public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'user');
            if($result !== true) {
                return $this->error($result);
            }
            unset($data['id']);
            $data['etime'] = strtotime($data['etime']);
            $data['password'] = md5($data['password']);
            $data['ctime'] = time();
            if (!UserModel::create($data)) {
                return $this->error('添加失败！');
            }
            return $this->success('添加成功。');
        }
		return $this->fetch('form');
	}
    /**
     * 删除用户
     * @return mixed
     */
    public function del(){
        $id = input('param.id/a');
        $model = new UserModel();
        if ($model->del($id)) {
            return $this->success('删除成功。');
        }
        return $this->error($model->getError());
    }
    /**
     * 修改用户
     * @return mixed
     */
	public function edit($id = 0){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'user');
            if($result !== true) {
                return $this->error($result);
            }
            $data['etime'] = strtotime($data['etime']);
            $data['password'] = md5($data['password']);
            if (!UserModel::update($data)) {
                return $this->error('修改失败！');
            }
            return $this->success('修改成功。');
        }
        $row = UserModel::where('id', $id)->field('id,card_id,group,name,mobile,password,status,etime')->find();
        $row['etime'] = date("Y-m-d",$row['etime']);
        $this->assign('data_info', $row);
        return $this->fetch('form');
	}

    /**
     *修改用户状态
     */
    public function editStatus(){
        $id = input('param.id');
        $val = input('param.val');
        $violation = new ViolationModel();
        $user = new UserModel();
        $uMap['id'] = $id;
        $vMap['userid'] = $id;
        $res = $user->status($uMap,$val);
        if ($val == 1){
            $violation->status($vMap,$val);
        }
        if ($res === false) {
            return $this->error('状态设置失败');
        }
        return $this->success('状态设置成功');
    }




}
?>